package com.system.couponserver.service.impl;

import com.system.couponserver.mapper.CouponMapper;
import com.system.couponserver.pojo.Coupon;
import com.system.couponserver.service.CouponService;
import com.system.couponserver.utils.ResultData;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {
    @Autowired(required = false)
    private CouponMapper couponMapper;

    /**
     * 全查
     * @param currentPsge
     * @param pageSize
     * @return
     */
    @Override
    public ResultData query01(int currentPsge, int pageSize) {
        int srartParam = (currentPsge-1)*pageSize;
        System.out.println(666);
        ResultData resultData = new ResultData();
        List<Coupon> list = couponMapper.query01(srartParam,pageSize);
        resultData.setData(list);
        // 返回总条数
        resultData.setCount(totalSize());
        //返回当前页码数
        resultData.setPages(currentPsge);
        //返回当前显示多少条
        resultData.setLimit(pageSize);
        return resultData;
    }


    /**
     * 条件查询 优惠卷id 优惠卷名字
     * @param coupon
     * @return
     */
    @Override
    public ResultData query03(Coupon coupon) {
        int currentpage = coupon.getCurrentPsge();
        int pageSize = coupon.getPageSize();
        int srartParam = (currentpage-1)*pageSize;
        coupon.setCurrentPsge(srartParam);
        ResultData resultData = new ResultData();
        List<Coupon> list = couponMapper.query03(coupon);
        System.out.println(srartParam);
        coupon.setCurrentPsge(0);
        coupon.setPageSize(totalSize());
        List<Coupon> list1 = couponMapper.query03(coupon);
        resultData.setData(list);
        // 返回总条数
        resultData.setCount(list1.size());
        //返回当前页码数
        resultData.setPages(currentpage);
        //返回当前显示多少条
        resultData.setLimit(pageSize);
        return resultData;
    }


    /**
     * 新增优惠卷
     */
    @Override
    public int query04(Coupon coupon) {
        coupon.setCoupon_Statue(1);
        return couponMapper.query04(coupon);
    }


    /**
     * 条件查询 通过id
     */
    @Override
    public ResultData query05(Coupon coupon) {
//        int currentpage = coupon.getCurrentPsge();
//        int pageSize = coupon.getPageSize();
//        int srartParam = (currentpage-1)*pageSize;
//        coupon.setCurrentPsge(srartParam);
        ResultData resultData = new ResultData();
        List<Coupon> list = couponMapper.query05(coupon);
//        coupon.setCurrentPsge(0);
//        coupon.setPageSize(totalSize());
//        List<Coupon> list1 = couponMapper.query05(coupon);
        resultData.setData(list);
        // 返回总条数
//        resultData.setCount(list1.size());
//        //返回当前页码数
//        resultData.setPages(currentpage);
//        //返回当前显示多少条
//        resultData.setLimit(pageSize);
        return resultData;
    }

    /**
     * 编辑修改
     */
    @Override
    public int query06(Coupon coupon) {
        return couponMapper.query06(coupon);
    }

    /**
     * 删除优惠卷
     * @param coupon
     * @return
     */
    @Override
    public int query07(Coupon coupon) {
        return couponMapper.query07(coupon);
    }


    /**
     * 下拉框 动态获取
     */
    @Override
    public List<Coupon> query08(Coupon coupon) {
        return couponMapper.query08(coupon);
    }

    /**
     * 返回总条数
     * @return
     */
    private int totalSize() {
        return couponMapper.query02();
    }
}
